package com.heima.search.service.impl;

import com.heima.common.constants.es.EsConstants;
import com.heima.common.exception.CustException;
import com.heima.es.service.EsService;
import com.heima.feigns.ArticleFeign;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDTO;
import com.heima.model.search.vos.SearchArticleVO;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.thread.AppThreadLocalUtils;
import com.heima.search.service.ApArticleSearchService;
import com.heima.search.service.ApUserSearchService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class ApArticleSearchServiceImpl implements ApArticleSearchService {
    @Autowired
    EsService esService;
    @Autowired
    ArticleFeign articleFeign;
    @Resource
    ApUserSearchService apUserSearchService;

    @Override
    public ResponseResult search(UserSearchDTO dto) {
        if (dto == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getPageSize() == 0){
            dto.setPageSize(10);
        }
        if (dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        ApUser user = AppThreadLocalUtils.getUser();
        if (user != null){
            dto.setEntryId(user.getId());
            apUserSearchService.insert(dto);
        }
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        设置布尔搜索
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.matchQuery("title",dto.getSearchWords()));
//        设置按照创建时间倒序
        searchSourceBuilder.sort("publishTime", SortOrder.DESC);
//        设置高亮字段
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("title").preTags("<font style='color: red; font-size: inherit;'>").postTags("</font>");
        searchSourceBuilder.highlighter(highlightBuilder);
//        设置查询范围
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(dto.getPageSize());
        searchSourceBuilder.query(boolQueryBuilder);
        PageResponseResult search = esService.search(searchSourceBuilder, SearchArticleVO.class, EsConstants.ARTICLE_INDEX_NAME);
        return search;
    }

    @Override
    public void saveArticle(SearchArticleVO article) {
        if (article == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        ResponseResult<ApArticle> responseResult = articleFeign.findArticleById(article.getId());
        if (responseResult.getCode() != 0){
            log.error("远程调用articleFeign.findArticleById出错");
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
        }
        ApArticle apArticle = responseResult.getData();
        if (apArticle == null){
            log.error("未查询到article");
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
        }
        BeanUtils.copyProperties(apArticle,article);

        esService.save(article,EsConstants.ARTICLE_INDEX_NAME);

        log.info("新增索引成功");
    }

    @Override
    public void deleteArticle(String articleId) {
        if (articleId == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        esService.deleteById(articleId,EsConstants.ARTICLE_INDEX_NAME);
        log.info("新删索引成功");
    }
}
